﻿using System;
using System.Collections.Generic;

// Write a program that extracts from a given text all palindromes, e.g. "ABBA", "lamal", "exe".

namespace ExtractPalindromes
{
    class ExtractPalindromes
    {
        static bool IsPalindrome(string word)
        {
            //If the first letter is equal to the last, second to the last minus one...
            for (int index = 0, length = word.Length / 2, wordLength = word.Length - 1; index < length; index++)
            {
                if (word[index] != word[wordLength - index])
                {
                    return false;
                }
            }

            return true;
        }

        static List<string> PalindromeExtractor(string text)
        {
            List<string> palindromes = new List<string>();
            string[] words = text.Split(' ');

            foreach (string word in words)
            {
                if (IsPalindrome(word))
                {
                    palindromes.Add(word);
                }
            }

            return palindromes;
        }

        static void Main()
        {
            List<string> palindromes = PalindromeExtractor("ABBA geeg ruffas sooos palindrom");

            foreach (string word in palindromes)
            {
                Console.WriteLine(word);
            }
        }
    }
}
